/*
 * TreeModelEvent.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.events;

import java.util.EventObject;

import com.cute4j.swing.tree.TreePath;
import com.cute4j.util.CommonConstants;

/**
 * Tree Model Event.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class TreeModelEvent extends EventObject {

// Private Static Fields. ------------------------------------------------------

  private static final long serialVersionUID = -1306278567647711041L;

// Protected Fields. -----------------------------------------------------------

  protected transient TreePath path;
  protected int[] childIndices;
  protected Object[] children;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new TreeModelEvent object.
   *
   * @param source the event source
   * @param path the path
   * @param childIndices the child indices
   * @param children the children
   *
   * @throws IllegalArgumentException if the source is <tt>null</tt>
   */
  public TreeModelEvent(Object source, TreePath path, int[] childIndices,
                        Object[] children) {
    super(source);
    this.path = path;
    if (childIndices != null) {
      this.childIndices = (int[])childIndices.clone();
    }
    if (childIndices != null) {
      this.children = (Object[])children.clone();
    }
  }

  /**
   * Constructs new TreeModelEvent object.
   *
   * @param source the event source
   * @param path the path
   *
   * @throws IllegalArgumentException if the source is <tt>null</tt>
   */
  public TreeModelEvent(Object source, TreePath path) {
    super(source);
    this.path = path;
    childIndices = CommonConstants.EMPTY_INT_ARRAY;
  }

  /**
   * Constructs new TreeModelEvent object.
   *
   * @param source the event source
   * @param path the path
   * @param childIndices the child indices
   * @param children the children
   *
   * @throws IllegalArgumentException if the source is <tt>null</tt>
   */
  public TreeModelEvent(Object source, Object[] path, int[] childIndices,
                        Object[] children) {
    this(source, new TreePath(path), childIndices, children);
  }

  /**
   * Constructs new TreeModelEvent object.
   *
   * @param source the event source
   * @param path the path
   *
   * @throws IllegalArgumentException if the source is <tt>null</tt>
   */
  public TreeModelEvent(Object source, Object[] path) {
    this(source, new TreePath(path));
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns three path.
   *
   * @return three path
   */
  public TreePath getTreePath() {
    return path;
  }

  /**
   * Returns path.
   *
   * @return path
   */
  public Object[] getPath() {
    if (path == null) {
      return CommonConstants.EMPTY_OBJECT_ARRAY;
    }
    return path.getPath();
  }

  /**
   * Returns children.
   *
   * @return children
   */
  public Object[] getChildren() {
    if (children == null) {
      return CommonConstants.EMPTY_OBJECT_ARRAY;
    }
    return (Object[])children.clone();
  }

  /**
   * Return child indices.
   *
   * @return child indices
   */
  public int[] getChildIndices() {
    if (childIndices == null) {
      return CommonConstants.EMPTY_INT_ARRAY;
    }
    return (int[])childIndices.clone();
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public String toString() {
    StringBuffer sb = new StringBuffer(120);
    sb.append(getClass().getName());
    sb.append(CommonConstants.STRING_SPACE);
    sb.append(hashCode());
    if (path != null) {
      sb.append(" path ");
      sb.append(path);
    }
    if (childIndices != null) {
      sb.append(" indices [ ");
      int cil = childIndices.length;
      for (int i = 0; i < cil; i++) {
        sb.append(childIndices[i]);
        sb.append(CommonConstants.STRING_SPACE);
      }
      sb.append(']');
    }
    if (children != null) {
      sb.append(" children [ ");
      int clen = children.length;
      for (int i = 0; i < clen; i++) {
        sb.append(children[i]);
        sb.append(CommonConstants.STRING_SPACE);
      }
      sb.append(']');
    }
    return sb.toString();
  }

}
